package com.ruoyi.teaching.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.teaching.domain.Student;
import com.ruoyi.teaching.domain.TaskStatus;
import com.ruoyi.teaching.domain.TeachingPlan;
import com.ruoyi.teaching.mapper.StudentMapper;
import com.ruoyi.teaching.mapper.TeachingPlanMapper;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.DailyTasksMapper;
import com.ruoyi.teaching.domain.DailyTasks;
import com.ruoyi.teaching.service.IDailyTasksService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 每日任务Service业务层处理
 *
 * @author sunshine
 * @date 2024-02-01
 */
@Service
public class DailyTasksServiceImpl implements IDailyTasksService
{
    @Autowired
    private DailyTasksMapper dailyTasksMapper;

    @Autowired
    private TeachingPlanMapper teachingPlanMapper;
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 查询每日任务
     *
     * @param id 每日任务主键
     * @return 每日任务
     */
    @Override
    public DailyTasks selectDailyTasksById(Long id)
    {
        return dailyTasksMapper.selectDailyTasksById(id);
    }

    /**
     * 查询每日任务列表
     *
     * @param dailyTasks 每日任务
     * @return 每日任务
     */
    @Override
    public List<DailyTasks> selectDailyTasksList(DailyTasks dailyTasks)
    {
        return dailyTasksMapper.selectDailyTasksList(dailyTasks);
    }

    /**
     * 新增每日任务
     *
     * @param dailyTasks 每日任务
     * @return 结果
     */
    @Override
    public int insertDailyTasks(DailyTasks dailyTasks)
    {
        dailyTasks.setCreateTime(DateUtils.getNowDate());
        return dailyTasksMapper.insertDailyTasks(dailyTasks);
    }

    /**
     * 修改每日任务
     *
     * @param dailyTasks 每日任务
     * @return 结果
     */
    @Override
    public int updateDailyTasks(DailyTasks dailyTasks)
    {
        dailyTasks.setUpdateTime(DateUtils.getNowDate());
        return dailyTasksMapper.updateDailyTasks(dailyTasks);
    }

    /**
     * 批量删除每日任务
     *
     * @param ids 需要删除的每日任务主键
     * @return 结果
     */
    @Override
    public int deleteDailyTasksByIds(Long[] ids)
    {
        return dailyTasksMapper.deleteDailyTasksByIds(ids);
    }

    /**
     * 删除每日任务信息
     *
     * @param id 每日任务主键
     * @return 结果
     */
    @Override
    public int deleteDailyTasksById(Long id)
    {
        return dailyTasksMapper.deleteDailyTasksById(id);
    }

    @Override
    public void createDailyTasks() {
        //1.查询到今日所有的（每个班）教学计划
//        List<TeachingPlan> teachingPlanList = teachingPlanMapper.selectList(new LambdaQueryWrapper<TeachingPlan>()
//                .eq(TeachingPlan::getDate, LocalDate.now().toString()).isNotNull(TeachingPlan::getDate));
        String now = LocalDate.now().toString();
        List<TeachingPlan> teachingPlanList = teachingPlanMapper.selectTeachingPlanListByDate(now);
        for(TeachingPlan te:teachingPlanList){
            if (ObjectUtils.isEmpty(te.getDay())){
                return;
            }
            Long classesId = te.getClassesId();
            List<Student> students = studentMapper.selectStudentByClassesId(classesId);
            for (Student s:students){
                DailyTasks dailyTasks = DailyTasks.builder()
                        .classesId(classesId)
                        .date(DateUtils.parseDate(te.getDate()))
                        .stage(te.getStage())
                        .courseContent(te.getCourseContent())
                        .studentId(s.getId())
                        .status(TaskStatus.NOT_START.value)
                        .build();
                List<DailyTasks> dailyTasks1 = dailyTasksMapper.selectDailyTasksList(dailyTasks);
                dailyTasksMapper.insertDailyTasks(dailyTasks);
            }
        }
    }

    @Override
    public void overdueDailies() {
//获取到昨天的日期
//        LocalDate localDate = LocalDate.now().minusDays(1);
//        Date date = DateUtils.toDate(localDate);
//        String yesterday = DateFormatUtils.format(date, "yyyy-MM-dd");
//
//        //将昨天未检查的&状态为已提交的任务默认设置为合格
//        dailyTasksMapper.updateQualifiedPass(yesterday, TaskQualified.PASS.getValue(), TaskStatus.TASK_STATUS_FINISHED.getValue());
//        //将昨天未提交的任务默认设置为不及格,并且设置过期
//        dailyTasksMapper.updateQualifiedUnqualified(yesterday, TaskQualified.UNQUALIFIED.getValue(),TaskStatus.TASK_STATUS_EXPIRED.getValue());
    }
}
